package com.cmuis.goodguide;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/*
 * This class parses the text sent from GoodGuide into a 
 * ProductList object which is displayed aestetically to the user.
 */
@SuppressWarnings("unused")
public class ProductListParser extends DefaultHandler {
	
	private boolean in_entity = false;
	private boolean in_id = false;
	private boolean in_name = false;
	private boolean in_url = false;
	private boolean in_rating = false;
	private boolean in_value = false;
    private boolean in_subratings = false;
	private boolean in_parents = false;
	private boolean in_imageurl = false;
	
	private boolean in_product = false;
	private boolean in_category = false;
	private boolean in_company = false;
	private boolean in_brand = false;
	
	private String name = "";
	private String value = "";
	
	
	private Product finalProduct;
	private List<Product> finalProducts;
	
	/**
	 * Get the Product from the parsing sequence. 
	 * @return A Product object containing parsed data
	 */
	public List<Product> getProducts()
	{
		return this.finalProducts;
	}
	
	@Override
	public void startDocument() throws SAXException {
		finalProduct = new Product();
		this.finalProducts = new ArrayList<Product>();
	}
	
	@Override
	public void endDocument() throws SAXException{
		//Nothing to do
	}
	
	@Override
	public void startElement(String namespaceURI, String localName, 
				String qName, Attributes atts) throws SAXException {
		if (localName.equals("entity")) {
			this.in_entity = true;
			String att = atts.getValue("entity_type");
			if(att.equalsIgnoreCase("product"))
			{
				this.in_product = true;
				finalProduct = new Product();
			}
			else if (att.equalsIgnoreCase("company"))
			{
				this.in_company = true;
			}
			else if (att.equalsIgnoreCase("brand"))
			{
				this.in_brand = true;
			}
			else if (att.equalsIgnoreCase("category"))
			{
				this.in_category = true;
			}
		}
		else if (localName.equals("id")){
			this.in_id = true;
		}
		else if (localName.equals("name")) {
			this.in_name = true;
		}
		else if (localName.equals("url")){
			this.in_url = true;
		}
		else if (localName.equals("image-url")){
			this.in_imageurl = true;
		}
		else if (localName.equals("rating")) {
			this.in_rating = true;
		}
		else if (localName.equals("value")){
			this.in_value = true;
		}
		else if (localName.equals("sub-ratings")) {
			this.in_subratings = true;
		}
		else if (localName.equals("parents")){
			this.in_parents = true;
		}
	}
	
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
				throws SAXException {
		if (localName.equals("entity") && !this.in_parents) {
			finalProducts.add(finalProduct);
		} else if (localName.equals("entity") && this.in_parents) {
			this.in_brand = false;
			this.in_company = false;
			this.in_category = false;
		}
		else if (localName.equals("id")){
			this.in_id = false;
		}
		else if (localName.equals("name")) {
			this.in_name = false;
		}
		else if (localName.equals("url")){
			this.in_url = false;
		}
		else if (localName.equals("rating")) {
			this.in_rating = false;
		}
		else if (localName.equals("image-url")){
			this.in_imageurl = false;
		}
		else if (localName.equals("value")){
			this.in_value = false;
		}
		else if (localName.equals("sub-ratings")) {
			this.in_subratings = false;
		}
		else if (localName.equals("parents")){
			this.in_parents = false;
		}
	}
	
	@Override
	public void characters(char ch[], int start, int length){
		//url
		if(this.in_url){
			finalProduct.setURL(new String(ch,start,length));
		}
		else if (this.in_imageurl)
		{
			finalProduct.setImage(new String(ch,start,length));
		}
		//brand id/company id/category id/id
		else if(this.in_id){
			if(this.in_brand)
			{
				finalProduct.setBrandId(new String(ch,start,length));
			}
			else if (this.in_category)
			{
				finalProduct.setCategoryId(new String(ch,start,length));
			}
			else if (this.in_company)
			{
				finalProduct.setCompanyId(new String(ch,start,length));
			}
			else 
			{
				finalProduct.setId(new String(ch,start,length));
			}
		}
		//name
		//brand name;
		//company name;
		//category name;
		else if(this.in_name && !this.in_rating)
		{
			if(this.in_brand)
			{
				finalProduct.setBrandName(new String(ch,start,length));
			}
			else if (this.in_category)
			{
				finalProduct.setCategoryName(new String(ch,start,length));
			}
			else if (this.in_company)
			{
				finalProduct.setCompanyName(new String(ch,start,length));
			}
			else 
			{
				finalProduct.setName(new String(ch,start,length));
			}
		}
		
		//overall rating
		//social rating;
		//environmental rating;
		//health rating;
		else if(this.in_rating)
		{
			if(this.in_name)
			{
				name =  new String(ch,start,length);
			}
			else if (this.in_value)
			{
				value = new String(ch,start,length);
				try{
					finalProduct.setRating(name, value);
				}
				catch (Exception e)
				{
					finalProduct.setRating(name, null);
				}
			}
		}
	} 
}
